﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace YouOweMe.BLL
{
    public class PeopleManager
    {
        /// <summary>
        /// get all people but the user
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public DAL.PersonCollection GetAllPeople(long userID)
        {
            DAL.PersonCollection persons = new DAL.PersonCollection();
            return persons.Where(DAL.Person.IdColumn.ColumnName, SubSonic.Comparison.NotEquals, userID).Load();
        }
        public DAL.PersonCollection GetAllPeople()
        {
            DAL.PersonCollection persons = new DAL.PersonCollection();
            return persons.Load();
        }
        public long GetPeopleID(string userName)
        {
            
            DAL.Person person = new DAL.Person(DAL.Person.UserNameColumn.ColumnName,userName);
            if (person.IsNew)
            {
                person = new DAL.Person();
                person.UserName = userName;
                person.Initials = "to update";
                person.Mail = "to update";
                person.Save();
            }
            return person.Id;
        }

        public void InsertOnePeople(string UserName, string Initials, string Mail)
        {

            //check if username is not in database !
            DAL.Person newPerson = new DAL.Person(DAL.Person.UserNameColumn.ColumnName, UserName);
            if (newPerson.IsLoaded)
            { 
                return; 
            }
            newPerson.UserName = UserName;
            newPerson.Initials = Initials;
            newPerson.Mail = Mail;
            newPerson.Save();
        }

        public void UpdateOnePeople(long ID, string Initials, string Mail)
        {
            DAL.Person newPerson = new DAL.Person(ID);
            if (newPerson.IsLoaded)
            {
                newPerson.Initials = Initials;
                newPerson.Mail = Mail;

                if (newPerson.IsDirty)
                    newPerson.Save();
            }
        }
    }
}
